package cn.kgc.mvc.servlet;

import cn.kgc.mvc.entity.Student;
import cn.kgc.mvc.service.StudentService;
import cn.kgc.mvc.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/5/4
 * @Description:  /student?method=add
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {

    private StudentService studentService = new StudentServiceImpl();

    //  get  post  delete
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String method = req.getParameter("method");
        switch (method){
            case "add":
                add(req,resp);
                break;
            case "delete":
                delete(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
            case "select":
                select(req,resp);
                break;
            case "toUpdate":
                toUpdate(req,resp);
                break;
        }
    }


    // 添加学生信息
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("=========add()===========");

        // 获取参数 String    数据类型转换      ->springmvc

        String name = req.getParameter("name");
        Integer age = Integer.valueOf(req.getParameter("age")) ;

        // 数据封装
        Student student = new Student();
        student.setAge(age);
        student.setName(name);

        Boolean b =   studentService.add(student);
        if(b){
            //重定向到主页
            resp.sendRedirect(req.getContextPath()+"/student?method=select");
        }else {
            req.setAttribute("msg","添加失败");
            req.getRequestDispatcher("/add.jsp").forward(req,resp);
        }

    }

    // 删除学生信息
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("=========delete()===========");
        int id = Integer.parseInt(req.getParameter("id"));
         Boolean b =  studentService.deleteById(id);
        HttpSession session = req.getSession();
        String id1 = session.getId();
        System.out.println("session id = " + id1);
        if(!b){
            session.setAttribute("msg","删除失败");
        }
        resp.sendRedirect(req.getContextPath()+"/student?method=select");
    }

    // 更新学生信息
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("=========update()===========");

        // 获取参数 String    数据类型转换      ->springmvc

        String name = req.getParameter("name");
        Integer age = Integer.valueOf(req.getParameter("age")) ;
        Integer id = Integer.valueOf(req.getParameter("id"));
        // 数据封装
        Student student = new Student();
        student.setAge(age);
        student.setName(name);
        student.setId(id);

        Boolean b =   studentService.update(student);
        if(b){
            //重定向到主页
            resp.sendRedirect(req.getContextPath()+"/student?method=select");
        }else {
            req.setAttribute("msg","更新失败");
            req.getRequestDispatcher("/update.jsp").forward(req,resp);
        }

    }

    protected void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取学生id  查询学生详细信息
        Integer id = Integer.valueOf(req.getParameter("id")) ;
        Student student =  studentService.getById(id);
        req.setAttribute("student",student);

        req.getRequestDispatcher("/update.jsp").forward(req,resp);  //  springmvc  视图名耦合
    }

    // 查询学生信息
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("=========select()===========");

        List<Student> students =  studentService.selectList();
        req.setAttribute("students", students);
        req.getRequestDispatcher("/student.jsp").forward(req,resp);

    }
}
